﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using SPDevLab.WebParts.JsAppWebPart.Apps;
using SPDevLab.WebParts.JsAppWebPart.Client;
using SPDevLab.WebParts.JsAppWebPart.Consts;
using SPDevLab.WebParts.JsAppWebPart.Services.Base;
using SPDevLab.WebParts.Extensions;

namespace SPDevLab.WebParts.JsAppWebPart
{
    public class JsApplicationEditorPart : EditorPart
    {
        #region contructors

        public JsApplicationEditorPart()
        {

        }

        #endregion

        #region properties

        private readonly HtmlGenericControl _appPropertyPanelControl = new HtmlGenericControl("div");
        private readonly HiddenField _appResultValueControl = new HiddenField();

        public string AppResultValueClientId
        {
            get
            {
                if (_appResultValueControl != null)
                    return _appResultValueControl.ClientID;

                return string.Empty;
            }
        }

        protected JsApplicationWebPart HostWebpart
        {
            get
            {
                return WebPartToEdit as JsApplicationWebPart;
            }
        }

        protected virtual JsAppServiceBase JsAppService
        {
            get
            {
                return HostWebpart.JsAppService;
            }
        }

        #endregion

        #region methods

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            InitWebpartStrings();
        }

        protected virtual void InitWebpartStrings()
        {
            Title = JsAppConsts.DefaultStrings.EditorPartTitle;
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Controls.Add(_appPropertyPanelControl);
            Controls.Add(_appResultValueControl);

            InvalidateJsAppContent();
        }

        public override bool ApplyChanges()
        {
            if (!string.IsNullOrEmpty(_appResultValueControl.Value))
            {
                var contextValue = JsAppService.DeserializeFromJson<JsAppOptionsResultContext>(_appResultValueControl.Value);

                HostWebpart.SetJsAppOptionsContext(contextValue.selectedAppTitle, contextValue.selectedAppFolderPath);
                InvalidateJsAppContent();
            }

            return true;
        }

        private void InvalidateJsAppContent()
        {
            if (SPContext.Current != null)
            {
                var site = SPContext.Current.Site;

                var appList = SPUrlUtility.CombineUrl(site.RootWeb.Url, JsAppConsts.Urls.AppLibraryUrl);
                var appFolder = SPUrlUtility.CombineUrl(appList, JsAppConsts.Folders.SystemFolderName + "/toolpart");

                var app = JsAppService.LoadLastActiveApp(site, appFolder);

                if (app != null && !string.IsNullOrEmpty(app.AppHtmlContent))
                {
                    var appTokens = new List<JsAppToken>();

                    appTokens.AddRange(JsAppService.GetLinkedJsAppTokens(site));
                    appTokens.AddRange(JsAppService.GetLinkedJsAppTokens(HostWebpart));
                    appTokens.AddRange(JsAppService.GetLinkedJsAppTokens(this));

                    _appPropertyPanelControl.InnerHtml = JsAppService.CreateAppHtmlContent(app.AppHtmlContent, appTokens);
                }
            }
        }

        public override void SyncChanges()
        {
            //throw new NotImplementedException();
        }

        #endregion
    }
}
